/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package extension.game.zone;

import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.extensions.BaseClientRequestHandler;
import extension.game.database.Database;
import extension.game.database.UserData;
import log.Debug;

/**
 *
 * @author daz
 */
public class MoneyJoinHandler extends BaseClientRequestHandler {

    @Override
    public void handleClientRequest(User user, ISFSObject params) {
        GameZoneExtension ext = (GameZoneExtension) getParentExtension();
        if (!Debug.isDebug) {
            long moneyJoin = 0;
            try {
                moneyJoin = params.getLong("money_join");
            } catch (Exception ex) {
                moneyJoin = params.getInt("money_join");
            }
            Database database = ext.getDatabase();
            UserData userData = database.getUserInfo(user.getName());
            long newMoney = userData.getMoney() - moneyJoin;
            long oldMoneyJoin = database.getMoneyJoin(ext.getRoomPrefix(), user.getName());
            database.insetMoneyQueue(user.getName(), newMoney + oldMoneyJoin, moneyJoin, ext.getRoomPrefix());
        }
        ext.send(Command.MONEY_JOIN, null, user);

    }
}
